using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Security.Cryptography;
using Microsoft.Win32;

namespace SPALM.SharePointSoftwareFactory.Library
{
    [Serializable()]
    public class SPSFLicense
    {
        public string Company { get; set; }
        public DateTime CreationDate { get; set; }
        public DateTime Expires { get; set; }
        public bool NeverExpires { get; set; }

        public bool IsExpressVersion()
        {
            return false;
            if (NeverExpires)
            {
                return false;
            }
            if (Expires >= DateTime.Now)
            {
                return false;
            }
            return true;
        }

        public bool IsDemoVersion()
        {
            return false;
            if (NeverExpires)
            {
                return false;
            }
            if (Expires > DateTime.Now)
            {
                return true;
            }
            return false;
        }

        public bool IsFullVersion()
        {
            if (NeverExpires)
            {
                return true;
            }
            return false;
        }

        public static SPSFLicense LoadLicense()
        {
            try
            {
                //find the license file
                //where is the package installed????
                RegistryKey gaxKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\VisualStudio\9.0\Menus\", false);
                string firstTemplate = gaxKey.GetValue("SharePointSoftwareFactory.Base").ToString();
                //return something like C:\SPALM\SharePointSoftwareFactory.Base\SharePointSoftwareFactory.Base\bin\Debug\SharePointSoftwareFactory.BaseUI.dll,1000,1

                string gaxDll = firstTemplate.Substring(0, firstTemplate.IndexOf(","));
                string gaxBaseDir = Directory.GetParent(gaxDll).FullName;
                string licenseFile = Path.Combine(gaxBaseDir, "license.txt");
                if (File.Exists(licenseFile))
                {
                    return ReadLicenseFile(File.ReadAllText(licenseFile));
                }
            }
            catch (Exception)
            {
            }

            SPSFLicense defaultLicense = new SPSFLicense();
            defaultLicense.Company = "default";
            defaultLicense.CreationDate = DateTime.Now;
            defaultLicense.Expires = DateTime.Now.AddDays(-100);
            defaultLicense.NeverExpires = false;
            return defaultLicense;
        }

        public string GetKey()
        {
            XmlSerializer formatter = new XmlSerializer(typeof(SPSFLicense));
            StringWriter writer = new StringWriter();
            MemoryStream stream = new MemoryStream();
            formatter.Serialize(writer, this);

            byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(writer.ToString());
            byte[] key = System.Text.ASCIIEncoding.ASCII.GetBytes("digital1");
            byte[] iv = System.Text.ASCIIEncoding.ASCII.GetBytes("steria12");

            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            cryptoProvider.Key = key;
            cryptoProvider.IV = iv;
            ICryptoTransform Itransform = cryptoProvider.CreateEncryptor();

            return Convert.ToBase64String(Itransform.TransformFinalBlock(data, 0, data.Length));
        }

        public static SPSFLicense ReadLicenseFile(string licenseKey)
        {
            byte[] key = System.Text.ASCIIEncoding.ASCII.GetBytes("digital1");
            byte[] iv = System.Text.ASCIIEncoding.ASCII.GetBytes("steria12");

            byte[] buffer = Convert.FromBase64String(licenseKey);
            DESCryptoServiceProvider cryptoProvider2 = new DESCryptoServiceProvider();
            cryptoProvider2.Key = key;
            cryptoProvider2.IV = iv;
            ICryptoTransform Itransform2 = cryptoProvider2.CreateDecryptor();

            string resultXml = Encoding.ASCII.GetString(Itransform2.TransformFinalBlock(buffer, 0, buffer.Length));

            StringReader reader = new StringReader(resultXml);
            XmlSerializer deserializer = new XmlSerializer(typeof(SPSFLicense));
            SPSFLicense decodeLicense = (SPSFLicense)deserializer.Deserialize(reader);
            return decodeLicense;
        }
    }
}
